package com.robot.mannge.common.config.security;

import com.robot.mannge.auth.domain.vo.SysUserVO;
import com.robot.mannge.auth.service.SysUserService;
import com.robot.mannge.common.exception.KabobExceptionUtil;
import com.robot.mannge.common.result.ResultCode;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.PrintStream;

/**
 * @Author: Smile
 * @Date: 2024/01/30/11:38
 * @Description:
 */

/**
 * springSecruty的用户认证处理类
 */

@Component
public class CustomerUserDetailsService implements UserDetailsService {

    @Resource
    private SysUserService sysUserService;

    /**
     * 用户认证
     *
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //查找用户是否存在
        SysUserVO sysUserVO = sysUserService.findUserByUserName(username);
        if (sysUserVO == null){
            KabobExceptionUtil.throwError(ResultCode.ERROR,"用户名或密码错误");
        }
        return sysUserVO;
    }
}
